<?php
/**
 * Created by PhpStorm.
 * User: manageistrator
 * Date: 2017/2/27
 * Time: 17:21
 */

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\Manage\BaseController;
use App\Repositories\Manage\UserRepo;
use Illuminate\Http\Request;
use App\Http\Requests;

class UserController extends BaseController
{
    public $menuId = 1;

    public function userList(UserRepo $userRepo, Request $request)
    {
        $this->actionAuthorize($this->menuId, 'userList');
        $data = $userRepo->getUserList();
        return view('manage.user.user_list', ['data' => $data]);

    }

    public function userRoleAdd(UserRepo $userRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'userRoleAdd');
        $uid = $request->get('uid');
        $data = $userRepo->getUser('uid = ?', [$uid]);
        $roleData = $userRepo->getRoleData();
        $role = $userRepo->getUserRole('user_id = ?', [$uid]);
        return view('manage.user.user_role_add', ['data' => $data, 'roleData' => $roleData, 'role' => $role]);

    }


    public function userRolePostAdd(UserRepo $userRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'userRoleAdd');
        $uid = $request->get('uid');
        $roleId = $request->get('roleId');
        if (empty($uid)) {
            return $this->ajaxResponse(1, '缺少用户id');
        }
        if (empty($roleId)) {
            return $this->ajaxResponse(1, '缺少角色id');
        }

        try {
            $userRepo->addUserRol($uid, $roleId);
            return $this->ajaxResponse(0, '添加成功');
        } catch (\Exception $e) {
            return $this->ajaxResponse(1, '添加失败' . $e->getMessage());
        }
    }

    public function userAdd(UserRepo $userRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'userAdd');
        return view('manage.user.user_add');
    }

    public function userPostAdd(UserRepo $userRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'userAdd');
        $name = $request->get('name');
        $pass = $request->get('pass');
        if (empty($name)) {
            return $this->ajaxResponse(1, '请填写用户名');
        }
        if (empty($pass)) {
            return $this->ajaxResponse(1, '请填写密码');
        }
        $pass = md5($pass);
        try {
            $userRepo->addUser($name, $pass);
            return $this->ajaxResponse(0, '添加成功');
        } catch (\Exception $e) {
            return $this->ajaxResponse(1, '添加失败' . $e->getMessage());
        }

    }

    public function userEdit(UserRepo $userRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'userEdit');
        $uid = $request->get('uid');
        $userData = $userRepo->getUser('uid = ?', [$uid]);
        return view('manage.user.user_edit', ['userData' => $userData]);
    }

    public function userPostEdit(UserRepo $userRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'userEdit');
        $uid = $request->get('uid');
        $name = $request->get('name');
        $pass = $request->get('pass');
        if (empty($name)) {
            return $this->ajaxResponse(1, '请填写用户名');
        }
        if (empty($pass)) {
            return $this->ajaxResponse(1, '请填写密码');
        }
        $pass = md5($pass);
        try {
            $userRepo->editUser($name, $pass, $uid);
            return $this->ajaxResponse(0, '修改成功');
        } catch (\Exception $e) {
            return $this->ajaxResponse(1, '修改失败' . $e->getMessage());
        }

    }


}